<template>
  <div class="base-info-view">
    <el-form
      class="form-detail"
      label-position="right"
      label-width="120px"
    >
      <el-row
        :gutter="20"
        style="width:100%"
      >
        <el-col :span="8">
          <el-form-item
            label="流程编号："
            style="width: 100%;"
          >
            <span>{{ detailData.processNo }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="创建时间："
            style="width: 100%;"
          >
            {{ dateFormat('yyyy-mm-dd HH:MM', detailData.startTime) }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="创建人："
            style="width: 100%;"
          >
            <span>{{ detailData.initiator }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div
      v-if="detail"
      class="distributor-info"
    >
      <item-title title="独家协议" />
      <el-form
        class="form-detail"
        label-position="right"
        label-width="120px"
        style="padding-left:20px;"
      >
        <el-row
          :gutter="20"
          style="width:100%"
        >
          <el-col :span="8">
            <el-form-item label="产品信息：">
              <div>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="toProductDetail"
                >{{ getProductName(detail) }}</el-link>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="交易价：">
              <div>{{ detail.tradePrice }}元/{{ getLabel('sell_unit',detail.sellUnit) }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="可营销终端：">
              {{ getLabel('app_mode', detail.terminalType) }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责营销职员：">
              {{ detail.headName }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <item-title title="配送信息" />
      <el-form
        class="form-detail"
        label-position="right"
        label-width="120px"
        style="padding-left:20px;"
      >
        <el-row
          :gutter="20"
          style="width:100%"
        >
          <el-col :span="8">
            <el-form-item label="配送模式：">
              <div>{{ detail.rddType == 2 ? '配送商配送' : '厂家配送' }}</div>
            </el-form-item>
          </el-col>
          <el-col
            v-if="detail.rddType == 2"
            :span="8"
          >
            <el-form-item label="配送商名称：">
              <div>{{ detail.distributeName }}</div>
            </el-form-item>
          </el-col>

          <el-col
            v-if="detail.rddType == 2"
            :span="8"
          >
            <el-form-item label="配送费：">
              <el-link
                type="primary"
                :underline="false"
                @click="toDistributeServer"
              >
                <div>
                  <div v-if="detail.distributeServer?.computeMode==='固定价格'">
                    <span>{{ detail.distributeServer?.distributePrice }}元</span>
                    <span v-if="detail.sellUnit">/{{ getLabel('sell_unit',detail.sellUnit) }}</span>
                  </div>
                  <div v-if="detail.distributeServer?.computeMode==='终端价百分比'">
                    <span>{{ detail.distributeServer?.distributePrice }}%</span>
                    <span>终端价</span>
                  </div>
                </div>
              </el-link>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="配送服务编号：">
              <div>{{ detail.distributeServer?.distributeServeNo }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="配送地区：">
              <area-list-view :areas="detail.distributeServer?.region" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="服务结束日期：">
              <div>{{ dateFormat('yyyy-mm-dd HH:MM', detail.distributeServer?.expireTime) }}</div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div
      v-if="detail"
      class="distributor-terminal"
    >
      <item-title title="配送终端信息" />
      <el-table
        :data="detail.terminals"
        :max-height="400"
        size="small"
        header-cell-class-name="table-header"
        border
      >
        <el-table-column
          prop="terminalName"
          label="终端名称"
          header-align="center"
          :min-width="160"
          show-overflow-tooltip
        >
          <template #default="scope">
            <el-link
              type="primary"
              :underline="false"
              @click="toTerminalDetail(scope.row)"
            >{{ scope.row.terminalName }}</el-link>
          </template>
        </el-table-column>
        <el-table-column
          label="终端所在地区"
          header-align="center"
          :min-width="160"
          show-overflow-tooltip
        >
          <template #default="scope">
            <div>
              <span v-if="scope.row.provinceName">{{ scope.row.provinceName }}</span>
              <span v-if="scope.row.cityName">{{ scope.row.provinceName&&scope.row.cityName?' / ':'' }}{{ scope.row.cityName }}</span>
              <span v-if="scope.row.areaName">{{ scope.row.cityName&&scope.row.areaName?' / ':'' }}{{ scope.row.areaName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="终端类型"
          header-align="center"
          align="center"
          :min-width="160"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{ getLabel('app_mode', detail.terminalType) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="terminalPrice"
          label="终端价（元）"
          header-align="center"
          align="center"
          :min-width="100"
          show-overflow-tooltip
        />
      </el-table>
    </div>
    <!-- 产品详情 -->
    <detail-drawer
      ref="proDetailDrawer"
      custom-title
    >
      <template #title>
        <span>产品详情 {{ getProductName(detail) }}</span>
      </template>
      <template #contents>
        <div class="drawer-content">
          <div class="history-list">
            <item-title title="基本信息" />
            <product-base-info
              v-if="detail.productId"
              :id="detail.productId"
            />
          </div>
          <div
            v-if="detail?.productId"
            class="history-list"
          >
            <item-title title="修改记录" />
            <operation-view
              :id="detail?.productId"
              mode="product"
            />
          </div>
        </div>
      </template>
    </detail-drawer>
    <!--合同详情-->
    <detail-drawer
      ref="contractDrawer"
      custom-title
      :size-width="1000"
    >
      <template #title>
        <span>合同 {{ detail?.contractNumber }} </span>
      </template>
      <template
        v-if="contractId"
        #contents
      >
        <div class="drawer-content">
          <div class="history-list">
            <contract-detail
              ref="salesDetailDrawer"
              :contract-id="contractId"
            />
          </div>
        </div>
      </template>
    </detail-drawer>
    <!--终端详情-->
    <detail-drawer
      ref="terminalDrawer"
      custom-title
      :size-width="1000"
    >
      <template #title>
        <span>终端 {{ rowData?.terminalName }} </span>
      </template>
      <template
        v-if="terminalId"
        #contents
      >
        <div class="drawer-content">
          <div class="history-list">
            <terminal-info :id="terminalId" />
          </div>
        </div>
      </template>
    </detail-drawer>
    <!--渠道客户详情-->
    <detail-drawer
      ref="agentDrawer"
      custom-title
    >
      <template #title>
        <span>渠道客户 - {{ rowData?.agentName }}</span>
      </template>
      <template #contents>
        <div class="drawer-content">
          <div class="history-list">
            <agent-info :id="agentId" />
          </div>
        </div>
      </template>
    </detail-drawer>
    <!--配送服务详情-->
    <detail-drawer
      ref="distributeServerDrawer"
      custom-title
    >
      <template #title>
        <span>配送服务 - {{ detail?.distributeServer?.distributeServeNo }}</span>
      </template>
      <template #contents>
        <div class="drawer-content">
          <div class="history-list">
            <distribute-service-info :id="detail.distributeServer?.id" />
          </div>
        </div>
      </template>
    </detail-drawer>
  </div>
</template>

<script>
import {defineAsyncComponent, reactive, ref, toRefs, watch} from 'vue'

export default {
  name: 'DistributorServiceInfo',
  components: {
    productBaseInfo: defineAsyncComponent(() =>
      import(
        '@CRM/views/commonView/detailComponents/product/productBaseInfo.vue'
      )
    ),
    contractDetail: defineAsyncComponent(() =>
      import('@CRM/views/commonView/contract/contractDetail.vue')
    ),
    terminalInfo: defineAsyncComponent(() =>
      import('@CRM/views/commonView/detailComponents/terminal/terminalInfo.vue')
    ),
    agentInfo: defineAsyncComponent(() =>
      import('@CRM/views/commonView/detailComponents/agent/agentInfo.vue')
    ),
    distributeServiceInfo: defineAsyncComponent(() =>
      import(
        '@CRM/views/commonView/detailComponents/distributor/distributorServiceInfo.vue'
      )
    ),
  },
  props: {
    detailData: {
      type: Object,
      default: () => {},
    },
  },
  setup(props, context) {
    var proDetailDrawer = ref(null)
    var contractDrawer = ref(null)
    var terminalDrawer = ref(null)
    var agentDrawer = ref(null)
    var distributeServerDrawer = ref(null)
    var reactiveData = reactive({
      detail: {},
      detailData: {},
      productId: 0,
      contractId: 0,
      terminalId: 0,
      agentId: 0,
      distributeServerId: 0,
      rowData: null,
    })
    const toProductDetail = () => {
      reactiveData.productId = reactiveData.detail.DisServeResponse?.productId
      proDetailDrawer.value.open()
    }
    const toContractDetail = () => {
      reactiveData.contractId = reactiveData.detail.contractId
      contractDrawer.value.open()
    }
    const toTerminalDetail = (row) => {
      reactiveData.rowData = row
      reactiveData.terminalId = row.terminalId
      terminalDrawer.value.open()
    }
    const toAgentDetail = (row) => {
      reactiveData.rowData = row
      reactiveData.agentId = row.agentId
      agentDrawer.value.open()
    }
    const toDistributeServer = (row) => {
      reactiveData.distributeServerId = reactiveData.detail.DisServeResponse?.id
      distributeServerDrawer.value.open()
    }
    watch(
      () => props.detailData,
      (newval, oldval) => {
        if (newval) {
          reactiveData.detailData = newval
          reactiveData.detail = newval.data
          console.log(232)
        }
      },
      { immediate: true, deep: true }
    )
    return {
      ...toRefs(props),
      ...toRefs(reactiveData),
      proDetailDrawer,
      contractDrawer,
      terminalDrawer,
      agentDrawer,
      distributeServerDrawer,
      toProductDetail,
      toContractDetail,
      toTerminalDetail,
      toAgentDetail,
      toDistributeServer,
    }
  },
}
</script>

<style scoped>
</style>
